package com.scrx.ymg.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.scrx.ymg.entity.Address;
import com.scrx.ymg.entity.BankAccount;
import com.scrx.ymg.entity.User;
import com.scrx.ymg.service.address.IAddressService;
import com.scrx.ymg.service.supermarket.ISuperMarketService;
import com.scrx.ymg.service.user.IUserService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

@Controller
@RequestMapping("/sMSVerificationAction")
public class SMSVerificationAction {

	@Autowired
	private IUserService users;
	
	@Autowired
	private ISuperMarketService sms;
	
	@Autowired
	private IAddressService adds;

	/**
	 * 地址注册发送手机验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/sendNote", produces = "text/json;charset=UTF-8")
	public String sendNote(String recNum, int userId) {
		try {
			User user = users.findById(userId);
			if (user != null) {
				int code = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
				user.setCode(code);
				user.setPhone(recNum);
				String msg = users.updateUser(user);
				if (msg.equals("true")) {
					TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest", "23755969",
							"f801d19589919b93f8a60ffb560c9be4");
					AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
					req.setSmsType("normal");
					req.setSmsFreeSignName("乙秒购商城");
					req.setSmsParamString("{\"code\":\"" + code + "\"}");
					req.setRecNum(recNum);
					req.setSmsTemplateCode("SMS_14186248");
					AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	/**
	 * 商家绑定账户验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/merchantNote", produces = "text/json;charset=UTF-8")
	public String merchantNote(BankAccount bank) {
		try {
			User user = users.findById(Integer.parseInt(bank.getOpeningBank()));
			if (user != null) {
				int code = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
				user.setCode(code);
				String msg = users.updateUser(user);
				if (msg.equals("true")) {
					TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest", "23755969",
							"f801d19589919b93f8a60ffb560c9be4");
					AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
					req.setSmsType("normal");
					req.setSmsFreeSignName("乙秒购商城");
					req.setSmsParamString("{\"code\":\"" + code + "\"}");
					req.setRecNum(bank.getBankCard());
					req.setSmsTemplateCode("SMS_62345130");
					AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
					System.out.println(rsp.getBody());
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 创建商家账户
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/merchantverificationCode", produces = "text/json;charset=UTF-8")
	public String merchantverificationCode(BankAccount bank) {
		User user = users.findById(Integer.parseInt(bank.getOpeningBank()));
		String msg="false";
		if(user!=null){
			if(bank.getCode().equals(user.getCode().toString())){
				bank.setIdCard(user.getUserId());
				msg = sms.createBankAccountInfo(bank);
			}else{
				return "251";
			}
		}
		return msg;
	}
	/**
	 * 验证手机验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/verificationCode", produces = "text/json;charset=UTF-8")
	public String verificationCode(String code,String phone, int userId,String addjson) {
		User user = users.findById(userId);
		Gson gson = new Gson();
		//if(code.equals(user.getCode().toString())&&phone.equals(user.getPhone())){
			Address add=gson.fromJson(addjson, Address.class);
			//String msg = adds.save(add);
			user.setPhone(phone);
			String msg =adds.saveAddressAndUpdateUser(add,user);
			if(msg.equals("true")){
				return gson.toJson(add);
			}
			return "false";
		
	}
	

}
